package com.dietdroid;
/*
 * MainActivity.java
 * Author: Brian Adams
 * 
 * This class is for the main activity screen. 
 */


import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	String PREFERENCES = "DIETDROID";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        
        // Scan Button listener
        Button scanButton = (Button) findViewById(R.id.buttonScan);
        //scanButton.getBackground().setColorFilter(new LightingColorFilter(0x000000, 0x33CC33));
        scanButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	IntentIntegrator.initiateScan(MainActivity.this);
            }

        });
        
        // Search Button listener
        Button searchButton = (Button) findViewById(R.id.buttonSearch);
        //searchButton.getBackground().setColorFilter(new LightingColorFilter(0x000000, 0x33CC33));
        searchButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Bundle b = new Bundle();
            	b.putBoolean("error", false);
    			Intent manualSearch = new Intent(MainActivity.this, SearchActivity.class);
    			manualSearch.putExtras(b);
    			startActivity(manualSearch);
            }

        });
        // History Button listener
        Button historyButton = (Button) findViewById(R.id.buttonHistory);
        historyButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), HistoryViewActivity.class);
                startActivityForResult(myIntent, 0);
            }

        });
        
        // Profile Button listener
        Button profileButton = (Button) findViewById(R.id.buttonSettings);
        profileButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), MedicalProfileActivity.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }
    
    @Override
    public void onStart() {
    	super.onStart();
		SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
		boolean firstUse = sharedPreferences.getBoolean("FIRST_USE", false);
		if(!firstUse) {
			buildFirstUseDialog();
			SharedPreferences.Editor editor = sharedPreferences.edit();
			editor.putBoolean("FIRST_USE", true);
			editor.commit();
		}

	}
    
    // Barcode Scanned Listener
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    	IntentResult res = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    	if (res != null) {
    		Bundle b = new Bundle();
    		if (resultCode == RESULT_OK) {
    			// Handle successful scan
    			b.putString("barcode", res.getContents());
    			b.putString("format", res.getFormatName());
        		Intent results = new Intent(MainActivity.this, ResultListActivity.class);
        		results.putExtras(b);
        		startActivity(results);
    		} 
    	}
    }
    
    private void buildFirstUseDialog() {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.first_use_dialog);
		dialog.setTitle("Welcome!");
		dialog.setCancelable(true);

		// set up text
		TextView text = (TextView) dialog.findViewById(R.id.firstUseTxt);
		text.setText(R.string.firstUse);

		// set up button
		Button button = (Button) dialog.findViewById(R.id.firstUseBut);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				dialog.dismiss();
			}
		});
		dialog.show(); 
    }
    
}